package cn.wolfcode.wolf2w.conteoller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.search.domain.DestinationEs;
import cn.wolfcode.wolf2w.search.domain.StrategyEs;
import cn.wolfcode.wolf2w.search.domain.TravelEs;
import cn.wolfcode.wolf2w.search.domain.UserInfoEs;
import cn.wolfcode.wolf2w.search.service.IDestinationEsService;
import cn.wolfcode.wolf2w.search.service.IStrategyEsService;
import cn.wolfcode.wolf2w.search.service.ITravelEsService;
import cn.wolfcode.wolf2w.search.service.IUserInfoEsService;
import cn.wolfcode.wolf2w.search.vo.SearchResult;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DataInitController {
    //Mongodb服务
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IUserInfoService userInfoService ;
    @Autowired
    private ITravelService travelService ;
    @Autowired
    private IDestinationService destinationService ;
    //es服务
    @Autowired
    private IStrategyEsService strategyEsService;
    @Autowired
    private ITravelEsService travelEsService ;
    @Autowired
    private IDestinationEsService destinationEsService;
    @Autowired
    private IUserInfoEsService userInfoEsService ;


    @GetMapping("/dataInit")
    public Object initDate(){
        //将mongodb中的部分数据加载到es中
        //攻略
        List<Strategy> list = strategyService.list();
        StrategyEs strategyEs = new StrategyEs();
        for (Strategy strategy : list) {
            BeanUtils.copyProperties(strategy,strategyEs );
            strategyEsService.save(strategyEs);
        }
        //游记
        List<Travel> list1 =travelService.list();
        TravelEs tes = new TravelEs();
        for (Travel travel : list1) {
            BeanUtils.copyProperties(travel,tes );
            travelEsService.save(tes);
        }
        //用户
        List<UserInfo> uf = userInfoService.list();
        for (UserInfo u : uf) {
            UserInfoEs es = new UserInfoEs();
            BeanUtils.copyProperties(u, es);
            userInfoEsService.save(es);
        }

        //目的地
        List<Destination> dests = destinationService.list();
        for (Destination d : dests) {
            DestinationEs es = new DestinationEs();
            BeanUtils.copyProperties(d, es);
            destinationEsService.save(es);
        }
        return "ok" ;
    }
}
